package com.huawei.zyb.taskManager.domain.developer.entity;

import com.huawei.zyb.taskManager.domain.developer.valueobject.*;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 开发者聚合根
 * 管理开发者信息和状态
 */
public class Developer {
    private DeveloperId id;
    private DeveloperName name;
    private DeveloperRole role;
    private DeveloperStatus status;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    // 私有构造函数，强制使用工厂方法
    private Developer() {}

    /**
     * 创建新开发者
     */
    public static Developer create(
            DeveloperId id,
            DeveloperName name,
            DeveloperRole role) {
        
        Developer developer = new Developer();
        developer.id = id;
        developer.name = name;
        developer.role = role;
        developer.status = DeveloperStatus.ACTIVE;
        developer.createdAt = LocalDateTime.now();
        developer.updatedAt = LocalDateTime.now();
        
        return developer;
    }

    /**
     * 从数据库数据恢复开发者对象
     */
    public static Developer restore(
            DeveloperId id,
            DeveloperName name,
            DeveloperRole role,
            DeveloperStatus status,
            LocalDateTime createdAt,
            LocalDateTime updatedAt) {
        
        Developer developer = new Developer();
        developer.id = id;
        developer.name = name;
        developer.role = role;
        developer.status = status;
        developer.createdAt = createdAt;
        developer.updatedAt = updatedAt;
        
        return developer;
    }

    /**
     * 更新开发者信息
     */
    public void updateInfo(DeveloperName name) {
        if (this.status == DeveloperStatus.INACTIVE) {
            throw new IllegalStateException("非活跃状态的开发者不能更新信息");
        }
        this.name = Objects.requireNonNull(name, "开发者姓名不能为空");
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 更新开发者角色
     */
    public void updateRole(DeveloperRole role) {
        if (this.status == DeveloperStatus.INACTIVE) {
            throw new IllegalStateException("非活跃状态的开发者不能更改角色");
        }
        this.role = Objects.requireNonNull(role, "开发者角色不能为空");
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 更新开发者状态
     */
    public void updateStatus(DeveloperStatus status) {
        this.status = Objects.requireNonNull(status, "开发者状态不能为空");
        this.updatedAt = LocalDateTime.now();
    }



    /**
     * 设置为忙碌状态
     */
    public void setBusy() {
        updateStatus(DeveloperStatus.BUSY);
    }

    /**
     * 设置为活跃状态
     */
    public void setActive() {
        updateStatus(DeveloperStatus.ACTIVE);
    }

    /**
     * 设置为请假状态
     */
    public void setOnLeave() {
        updateStatus(DeveloperStatus.ON_LEAVE);
    }

    /**
     * 设置为非活跃状态
     */
    public void setInactive() {
        updateStatus(DeveloperStatus.INACTIVE);
    }

    /**
     * 检查是否可以分配任务
     */
    public boolean canBeAssignedTasks() {
        return this.status.canBeAssignedTasks();
    }

    /**
     * 检查是否可以管理项目
     */
    public boolean canManageProject() {
        return this.role.canManageProject() && this.status.isAvailable();
    }

    /**
     * 检查是否可以分配任务给其他人
     */
    public boolean canAssignTasks() {
        return this.role.canAssignTasks() && this.status.canBeAssignedTasks();
    }

    /**
     * 检查是否为管理角色
     */
    public boolean isManagementRole() {
        return this.role.isManagementRole();
    }

    /**
     * 检查是否可用
     */
    public boolean isAvailable() {
        return this.status.isAvailable();
    }

    /**
     * 检查是否忙碌
     */
    public boolean isBusy() {
        return this.status.isBusy();
    }

    /**
     * 检查是否在请假
     */
    public boolean isOnLeave() {
        return this.status.isOnLeave();
    }

    // Getters
    public DeveloperId getId() { return id; }
    public DeveloperName getName() { return name; }
    public DeveloperRole getRole() { return role; }
    public DeveloperStatus getStatus() { return status; }
    public LocalDateTime getCreatedAt() { return createdAt; }
    public LocalDateTime getUpdatedAt() { return updatedAt; }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Developer developer = (Developer) obj;
        return Objects.equals(id, developer.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Developer{" +
                "id=" + id +
                ", name=" + name +
                ", role=" + role +
                ", status=" + status +
                "}";
    }
}